package com.solution.admin.product.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.solution.admin.product.entity.ProductCategoryEntity;
import com.solution.admin.product.entity.ProductCategoryPropertyTemplateEntity;
import com.solution.admin.product.service.ProductCategoryPropertyTemplateService;
import com.solution.admin.product.service.ProductCategoryService;
import com.solution.common.controller.BaseController;
import com.solution.common.page.JSONGrid;
import com.solution.common.page.PageBean;
import com.solution.common.page.PageParam;
import com.solution.common.utils.string.StringUtil;

/**
 * 商品类型关联属性模板web控制层
 * 
 * @author solin
 * @date 2017-10-11 18:19:37
 */
@RequestMapping("/productCategoryPropertyTemplate")
@Controller
public class ProductCategoryPropertyTemplateController extends BaseController {

	private Logger logger = LoggerFactory.getLogger(ProductCategoryPropertyTemplateController.class);

	@Autowired
	ProductCategoryService productCategoryService;

	@Autowired
	private ProductCategoryPropertyTemplateService productCategoryPropertyTemplateService;

	/**
	 * 根据商品分类ID获取商品属性模版列表
	 * 
	 * @Title: getPropertyTemplateListByCategoryId
	 * @Description:(描述)
	 * @param productCategoryId
	 * @return List<ProductCategoryPropertyTemplateEntity>
	 * @author xu_cc
	 * @throws Exception
	 *             exception
	 * @Date 创建时间：2017年10月13日 上午11:32:48
	 */
	@RequestMapping("/getPropertyTemplateListByCategoryId")
	@ResponseBody
	public JSONGrid getPropertyTemplateListByCategoryId(HttpServletRequest request, Long productCategoryId) {
		try {
			List<ProductCategoryPropertyTemplateEntity> list = productCategoryPropertyTemplateService
			        .getPropertyTemplateListByCategoryId(productCategoryId);
			Long categoryId = productCategoryId;
			while (!(null != list && list.size() > 0)) {
				// 如果当前分类未取到规格项，那就取父类的规格
				ProductCategoryEntity productCategory = productCategoryService.getProductCategoryById(categoryId);
				if (null != productCategory && null != productCategory.getParentCategoryId()) {
					categoryId = productCategory.getParentCategoryId();
					list = productCategoryPropertyTemplateService.getPropertyTemplateListByCategoryId(productCategory.getParentCategoryId());
				} else {
					return new JSONGrid();
				}
			}
			return new JSONGrid(list, list.size());
		} catch (Exception e) {
			e.printStackTrace();
			return new JSONGrid();
		}
	}

	/**
	 * 跳转商品类型关联属性模板查询页面
	 * 
	 * @param ProductCategoryPropertyTemplate
	 *            接收界面查询条件参数
	 * @param request
	 *            请求对象
	 * @return 返回对应的页面视图
	 * @author solin
	 * @date 2017-10-11 18:19:37
	 */
	@RequestMapping("/toListProductCategoryPropertyTemplatePage")
	public String toListProductCategoryPropertyTemplatePage(ProductCategoryPropertyTemplateEntity productCategoryPropertyTemplate,
	        HttpServletRequest request) {

		return "product/productCategoryPropertyTemplate_list";
	}

	/**
	 * 分页查询商品类型关联属性模板列表
	 * 
	 * @param productCategoryPropertyTemplate
	 *            接收界面查询条件参数
	 * @param request
	 *            请求对象
	 * @return 返回对应的页面视图
	 * @author solin
	 * @date 2017-10-11 18:19:37
	 */
	@RequestMapping("/listProductCategoryPropertyTemplatePage")
	@ResponseBody
	public JSONGrid listProductCategoryPropertyTemplatePage(ProductCategoryPropertyTemplateEntity productCategoryPropertyTemplate,
	        HttpServletRequest request) {
		int pageNum = Integer.parseInt(request.getParameter("page").toString());
		int numPerPage = Integer.parseInt(request.getParameter("rows").toString());
		PageParam pageParam = super.getPageParam(request);
		pageParam.setPageNum(pageNum);
		pageParam.setNumPerPage(numPerPage);

		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("productCategoryId", productCategoryPropertyTemplate.getProductCategoryId());
		try {
			PageBean pageBean = productCategoryPropertyTemplateService.listProductCategoryPropertyTemplatePage(pageParam, paramMap);
			return new JSONGrid(pageBean.getRecordList(), pageBean.getTotalCount());
		} catch (Exception e) {
			handleException("分页查询商品类型关联属性模板失败", logger, e);
		}
		return new JSONGrid();
	}

	/**
	 * 跳转新增页面
	 * 
	 * @return string 跳转页面路径
	 * @author solin
	 * @date 2017-10-11 18:19:37
	 */
	@RequestMapping("/toAddProductCategoryPropertyTemplate")
	public String toAddProductCategoryPropertyTemplate(HttpServletRequest request) {
		request.setAttribute("opFlag", "add");
		return "product/productCategoryPropertyTemplate_form";
	}

	/**
	 * 跳转编辑页面
	 * 
	 * @return string 跳转页面路径
	 * @author solin
	 * @date 2017-10-11 18:19:37
	 */
	@RequestMapping("/toUpdateProductCategoryPropertyTemplate")
	public String toUpdateProductCategoryPropertyTemplate(HttpServletRequest request) {
		// 编辑操作
		request.setAttribute("opFlag", "edit");
		long id = Long.valueOf(request.getParameter("id"));
		try {
			ProductCategoryPropertyTemplateEntity productCategoryPropertyTemplate = productCategoryPropertyTemplateService
			        .getProductCategoryPropertyTemplateById(id);
			request.setAttribute("productCategoryPropertyTemplate", productCategoryPropertyTemplate);
		} catch (Exception e) {
			handleException("跳转编辑商品类型关联属性模板页面错误", logger, e);
		}
		return "product/productCategoryPropertyTemplate_form";
	}

	/**
	 * 新增或保存商品类型关联属性模板
	 * 
	 * @param request
	 *            请求对象
	 * @param productCategoryPropertyTemplateEntity
	 *            商品类型关联属性模板实体
	 * @return
	 * @author solin
	 * @date 2017-10-11 18:19:37
	 */
	@RequestMapping("/saveOrUpdateProductCategoryPropertyTemplate")
	@ResponseBody
	public Map<String, Object> saveProductCategoryPropertyTemplate(HttpServletRequest request,
	        ProductCategoryPropertyTemplateEntity productCategoryPropertyTemplateEntity) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		int result = 0;
		try {
			String id = request.getParameter("id");
			if (StringUtil.isNotEmpty(id)) {
				result = productCategoryPropertyTemplateService.updateProductCategoryPropertyTemplate(productCategoryPropertyTemplateEntity);
			} else {
				productCategoryPropertyTemplateEntity.setCreater(this.getUserName());
				result = (int) productCategoryPropertyTemplateService.saveProductCategoryPropertyTemplate(productCategoryPropertyTemplateEntity);
				resultMap.put("id", result);
			}

		} catch (Exception e) {
			handleException("添加商品类型关联属性模板失败", logger, e);
		}
		resultMap.put("code", result);
		return resultMap;
	}

	/**
	 * 删除商品类型关联属性模板
	 * 
	 * @param request
	 * @param productCategoryPropertyTemplateEntity
	 * @return
	 * @author: solin
	 * @date: 2017年10月11日 下午8:39:38
	 */
	@RequestMapping("/removeProductCategoryPropertyTemplate")
	@ResponseBody
	public Map<String, Object> removeProductCategoryPropertyTemplate(HttpServletRequest request,
	        ProductCategoryPropertyTemplateEntity productCategoryPropertyTemplateEntity) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		int result = 0;
		try {
			result = productCategoryPropertyTemplateService.deleteProductCategoryPropertyTemplateById(productCategoryPropertyTemplateEntity.getId());
		} catch (Exception e) {
			handleException("添加商品类型关联属性模板失败", logger, e);
		}
		resultMap.put("code", result);
		return resultMap;
	}

}
